package com.liao.starapp;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.liao.starapp.star.ItemStarGridViewLayoutAdapter;
import com.liao.starapp.star.Star;
import com.liao.starapp.star.StarDetailActivity;
import com.liao.starapp.star.StarViewPagerAdapter;
import com.liao.starapp.utils.GetAssetsData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;

public class FragmentStarLayoutFragment extends Fragment implements ViewPager.OnPageChangeListener, AdapterView.OnItemClickListener, View.OnClickListener {

    private static final int ICON_NUMBER = 4;

    private ViewPager viewPagerStar;
    private LinearLayout layoutIcon;
    private GridView gridView;

    private ArrayList<Star> stars;

    private Context context;

    private ImageView[] icons;
    private ArrayList<View> views;
    private int[] images = new int[]{R.mipmap.pic_star, R.mipmap.pic_guanggao, R.mipmap.pic_star, R.mipmap.pic_guanggao};

    private boolean isRun = true;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int currentItem = viewPagerStar.getCurrentItem();
            viewPagerStar.setCurrentItem(++currentItem);
            return false;
        }
    });

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_star_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        changeViewPager();
    }

    private void initView(View view) {
        viewPagerStar = view.findViewById(R.id.viewPager_star);
        layoutIcon = view.findViewById(R.id.layout_icon);
        gridView = view.findViewById(R.id.gridView);
        viewPagerStar.addOnPageChangeListener(this);

        context = getContext();


        icons = new ImageView[2];
        views = new ArrayList<>();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = 10;
        for (int i = 0; i < ICON_NUMBER; i++) {
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(images[i]);
            views.add(imageView);
            ImageView icon = new ImageView(context);
            imageView.setOnClickListener(this);
            if (i >= 2) {
                continue;
            }
            icon.setImageResource(R.mipmap.point_normal);
            icon.setLayoutParams(params);
            layoutIcon.addView(icon);
            icons[i] = icon;
        }
        icons[0].setImageResource(R.mipmap.point_focus);

        StarViewPagerAdapter adapter = new StarViewPagerAdapter(context, views);
        viewPagerStar.setAdapter(adapter);

        stars = new ArrayList<>();
        getGridViewData();
        ItemStarGridViewLayoutAdapter gridVIewAdapter = new ItemStarGridViewLayoutAdapter(context, star.getStarinfo());
        gridView.setAdapter(gridVIewAdapter);

        gridView.setOnItemClickListener(this);

    }

    Star star;

    private void getGridViewData() {
//        try {
        String s = GetAssetsData.getGridViewText(context, "xzcontent/xzcontent.json");
        Gson gson = new Gson();
        star = gson.fromJson(s, Star.class);
//            JSONObject jsonObject = new JSONObject(s);
//            json(jsonObject);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
    }

//    private void json(JSONObject json) {
//        try {
//            JSONArray starinfo = json.getJSONArray("starinfo");
//            for (int i = 0; i < starinfo.length(); i++) {
//                JSONObject jsonObject = starinfo.getJSONObject(i);
//                String name = jsonObject.getString("name");
//                String logoname = jsonObject.getString("logoname");
//                String date = jsonObject.getString("date");
//                String td = jsonObject.getString("td");
//                String gw = jsonObject.getString("gw");
//                String yy = jsonObject.getString("yy");
//                String tz = jsonObject.getString("tz");
//                String zg = jsonObject.getString("zg");
//                String ys = jsonObject.getString("ys");
//                String zb = jsonObject.getString("zb");
//                String hm = jsonObject.getString("hm");
//                String js = jsonObject.getString("js");
//                String info = jsonObject.getString("info");
//                Star star = new Star();
//                star.setName(name);
//                star.setLogoname(logoname);
//                star.setDate(date);
//                star.setTd(td);
//                star.setGw(gw);
//                star.setYy(yy);
//                star.setTz(tz);
//                star.setZg(zg);
//                star.setYs(ys);
//                star.setZb(zb);
//                star.setHm(hm);
//                star.setJs(js);
//                star.setInfo(info);
//                star.setImgFileName("xzlogo/" + star.getLogoname() + ".png");
//                stars.add(star);
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//    }

    private void changeViewPager() {
        new Thread() {
            @Override
            public void run() {
                while (isRun) {
                    try {
                        sleep(3000);
                        handler.sendEmptyMessage(0x001);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < icons.length; i++) {
            if (position % icons.length == i) {
                icons[i % icons.length].setImageResource(R.mipmap.point_focus);
            } else {
                icons[i].setImageResource(R.mipmap.point_normal);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isRun = false;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startActivity(new Intent(context, StarDetailActivity.class).putExtra("star", star.getStarinfo().get(position)));
    }

    @Override
    public void onClick(View v) {

    }
}
